package com.settlement.system.parse.formula;

/**
 * 占位符处理工具
 *
 * @author Ximi
 * @since 2020/10/10
 */
public class PlaceHolderFormulaUtils {

    /**
     * 处理占位符的问题
     *
     * @param formula
     * @param placeHoldParse
     * @return
     */
    public static String parse(String formula, PlaceHoldParse placeHoldParse) {

        StringBuilder result = new StringBuilder();
        StringBuilder placeHold = new StringBuilder();
        for (int index = 0; index < formula.length(); index++) {
            char c = formula.charAt(index);
            if (c == '+' || c == '-' || c == '/' || c == '*') {
                result.append(c);
                Object value = placeHoldParse.getPlaceHoldValue(placeHold.toString());
                result.append(value);
                placeHold.setLength(0);
            }
        }

        if (placeHold.length() != 0) {
            Object value = placeHoldParse.getPlaceHoldValue(placeHold.toString());
            result.append(value);
        }

        return result.toString();
    }
}
